<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>JavaScript运动框架之速度时间版本 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,运动框架,">
  

  <meta name="description" content="一、JavaScript运动框架之速度版 1.1 运动框架的实现思路 运动，其实就是在一段时间内改变 left 、 right 、 width 、 height 、 opactiy 的值，到达目的地之后停止  位移  top,left 折叠  width,height 淡入淡出 opacity 时间有关系 setInterval setTimeout     用javascript直接获取行间样式">
<meta name="keywords" content="JavaScript,运动框架">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript运动框架之速度时间版本">
<meta property="og:url" content="http://blog.poetries.top/2017/01/12/js-animate/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、JavaScript运动框架之速度版 1.1 运动框架的实现思路 运动，其实就是在一段时间内改变 left 、 right 、 width 、 height 、 opactiy 的值，到达目的地之后停止  位移  top,left 折叠  width,height 淡入淡出 opacity 时间有关系 setInterval setTimeout     用javascript直接获取行间样式">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.922Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript运动框架之速度时间版本">
<meta name="twitter:description" content="一、JavaScript运动框架之速度版 1.1 运动框架的实现思路 运动，其实就是在一段时间内改变 left 、 right 、 width 、 height 、 opactiy 的值，到达目的地之后停止  位移  top,left 折叠  width,height 淡入淡出 opacity 时间有关系 setInterval setTimeout     用javascript直接获取行间样式">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#一、JavaScript运动框架之速度版"><span class="toc-text">一、JavaScript运动框架之速度版</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-运动框架的实现思路"><span class="toc-text">1.1 运动框架的实现思路</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-一些案例演示"><span class="toc-text">1.2 一些案例演示</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-2-1-运动之速"><span class="toc-text">1.2.1 运动之速</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#1-2-2-运动速度之封装1"><span class="toc-text">1.2.2 运动速度之封装1</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#1-2-3-运动速度之封装2–增加opacity"><span class="toc-text">1.2.3 运动速度之封装2–增加opacity</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-运动框架之应用"><span class="toc-text">1.3 运动框架之应用</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-3-1-分享按钮"><span class="toc-text">1.3.1 分享按钮</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#1-3-2运动框架之轮播图应用"><span class="toc-text">1.3.2运动框架之轮播图应用</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#1-3-2-1-焦点轮播–左右-无缝-速度版实现"><span class="toc-text">1.3.2.1 焦点轮播–左右-无缝-速度版实现</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#二、JavaScript运动框架之时间版"><span class="toc-text">二、JavaScript运动框架之时间版</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-关于运动"><span class="toc-text">2.1 关于运动</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-一些案例演示"><span class="toc-text">2.2 一些案例演示</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-运动框架之时间版本-借助animate一些函数实现–综合完整版"><span class="toc-text">2.3 运动框架之时间版本-借助animate一些函数实现–综合完整版</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#三、运动框架之时间速度版完整封装"><span class="toc-text">三、运动框架之时间速度版完整封装</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-js-animate" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">JavaScript运动框架之速度时间版本</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2017.01.12</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 4.6k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 25分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h3 id="一、JavaScript运动框架之速度版"><a href="#一、JavaScript运动框架之速度版" class="headerlink" title="一、JavaScript运动框架之速度版"></a>一、JavaScript运动框架之速度版</h3><hr>
<h4 id="1-1-运动框架的实现思路"><a href="#1-1-运动框架的实现思路" class="headerlink" title="1.1 运动框架的实现思路"></a>1.1 运动框架的实现思路</h4><hr>
<p>运动，其实就是在一段时间内改变 <code>left</code> 、 <code>right</code> 、 <code>width</code> 、 <code>height</code> 、 <code>opactiy</code> 的值，到达目的地之后停止</p>
<ul>
<li>位移  <code>top</code>,<code>left</code></li>
<li>折叠  <code>width</code>,<code>height</code></li>
<li>淡入淡出 <code>opacity</code></li>
<li>时间有关系<ul>
<li><code>setInterval</code></li>
<li><code>setTimeout</code></li>
</ul>
</li>
</ul>
<ul>
<li>用javascript直接获取行间样式很容易，但如果要获取非行间样式那我们只能借助函数了。我这里编写了一个名为getStyle的函数，专门处理取非行间的样式</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getStyle</span>(<span class="params">obj,attr</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="1-2-一些案例演示"><a href="#1-2-一些案例演示" class="headerlink" title="1.2 一些案例演示"></a>1.2 一些案例演示</h4><hr>
<h5 id="1-2-1-运动之速"><a href="#1-2-1-运动之速" class="headerlink" title="1.2.1 运动之速"></a>1.2.1 运动之速</h5><hr>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line"><span class="selector-id">#box</span> &#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">background</span>: red;</span><br><span class="line">	 <span class="attribute">position</span>: relative;</span><br><span class="line">	<span class="attribute">left</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> box = <span class="built_in">document</span>.getElementById(<span class="string">"box"</span>);</span><br><span class="line"><span class="keyword">var</span> speed = <span class="number">0</span>; <span class="comment">//步长</span></span><br><span class="line"><span class="keyword">var</span> target = <span class="number">600</span>;</span><br><span class="line"><span class="keyword">var</span> timer = <span class="literal">null</span>;</span><br><span class="line">timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> curr = <span class="built_in">parseInt</span>(getStyle(box,<span class="string">"left"</span>)); <span class="comment">//去除getStyle(box,"left")的单位</span></span><br><span class="line">	<span class="keyword">if</span>(curr == target)&#123;</span><br><span class="line">		clearInterval(timer);</span><br><span class="line">		speed = <span class="number">0</span>;</span><br><span class="line">		alert(<span class="string">"运动结束"</span>);</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		speed +=<span class="number">10</span>;</span><br><span class="line">		box.style.left = speed + <span class="string">"px"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line"></span><br><span class="line">&#125;,<span class="number">1000</span>/<span class="number">30</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//监控left的值的变化 怎么样拿到left的值</span></span><br><span class="line"><span class="comment">//alert(getComputedStyle(box)["width"]);</span></span><br><span class="line"><span class="comment">//alert(box.currentStyle["left"]);</span></span><br><span class="line"><span class="comment">// currentStyle --IE </span></span><br><span class="line"><span class="comment">// getComputedStyle -- 非IE</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getStyle</span>(<span class="params">obj,attr</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a href="http://codepen.io/poetries/pen/wgGqzr" target="_blank" rel="noopener">在线演示</a></p>
<h5 id="1-2-2-运动速度之封装1"><a href="#1-2-2-运动速度之封装1" class="headerlink" title="1.2.2 运动速度之封装1"></a>1.2.2 运动速度之封装1</h5><hr>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"ball"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line"><span class="selector-id">#ball</span> &#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">background</span>: blue;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> ball = <span class="built_in">document</span>.getElementById(<span class="string">"ball"</span>);</span><br><span class="line"></span><br><span class="line">ball.onmouseover = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="comment">//同时变换 用的最多</span></span><br><span class="line">	<span class="comment">//move(this,"width",500,10);</span></span><br><span class="line">	<span class="comment">//move(this,"height",500,10);</span></span><br><span class="line">	move(ball,&#123;<span class="string">"width"</span>:<span class="number">400</span>,<span class="string">"height"</span>:<span class="number">300</span>&#125;,<span class="number">10</span>);</span><br><span class="line">&#125;</span><br><span class="line">ball.onmouseout = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="comment">//move(this,"width",100,-10);</span></span><br><span class="line">	<span class="comment">//move(this,"height",100,-10);</span></span><br><span class="line">	move(ball,&#123;<span class="string">"width"</span>:<span class="number">100</span>,<span class="string">"height"</span>:<span class="number">100</span>&#125;,<span class="number">-10</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params">obj,json,speed</span>)</span>&#123;</span><br><span class="line">	clearInterval(obj.timer);</span><br><span class="line">	<span class="keyword">var</span> mark = <span class="literal">true</span>;</span><br><span class="line">	obj.timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">var</span> attr <span class="keyword">in</span> json)&#123;</span><br><span class="line">			<span class="keyword">var</span> curr = <span class="built_in">parseInt</span>(getStyle(obj,attr));</span><br><span class="line">			<span class="keyword">var</span> target = json[attr];</span><br><span class="line">			<span class="keyword">if</span>(curr != target)&#123;</span><br><span class="line">				obj.style[attr] = curr+speed+<span class="string">"px"</span>;</span><br><span class="line">				mark = <span class="literal">false</span>;</span><br><span class="line">		  &#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(mark)&#123;</span><br><span class="line">			clearInterval(obj.timer);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;,<span class="number">1000</span>/<span class="number">30</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getStyle</span>(<span class="params">obj,attr</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>需要注意的地方<ul>
<li>当需要两个动画的时候，会执行后面一个，解决办法如下，回调函数</li>
<li>当需要两个以上的时候，需要考虑是否可写一行代码变换多个属性</li>
<li>变换不一致的时候，定时器被提前清除</li>
</ul>
</li>
</ul>
<p><a href="http://codepen.io/poetries/pen/KazvNe" target="_blank" rel="noopener">在线演示</a></p>
<h5 id="1-2-3-运动速度之封装2–增加opacity"><a href="#1-2-3-运动速度之封装2–增加opacity" class="headerlink" title="1.2.3 运动速度之封装2–增加opacity"></a>1.2.3 运动速度之封装2–增加opacity</h5><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"ball"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line"><span class="selector-id">#ball</span> &#123;</span><br><span class="line">  <span class="attribute">position</span>: relative;</span><br><span class="line">  <span class="attribute">left</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">top</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">background</span>: blue;</span><br><span class="line">  <span class="attribute">opacity</span>: <span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> ball = <span class="built_in">document</span>.getElementById(<span class="string">"ball"</span>);</span><br><span class="line">ball.onmouseover = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	move(ball,&#123;<span class="string">"width"</span>:<span class="number">300</span>,<span class="string">"height"</span>:<span class="number">300</span>,<span class="string">"opacity"</span>:<span class="number">0.3</span>&#125;);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//			ball.onmouseout = function()&#123;</span></span><br><span class="line"><span class="comment">//				move(ball,&#123;"width":100,"height":100&#125;,-10);</span></span><br><span class="line"><span class="comment">//			&#125;</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params">obj,json</span>)</span>&#123;</span><br><span class="line">	clearInterval(obj.timer);</span><br><span class="line">	<span class="keyword">var</span> mark = <span class="literal">true</span>;</span><br><span class="line">	obj.timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">var</span> attr <span class="keyword">in</span> json)&#123;</span><br><span class="line">			<span class="keyword">var</span> curr = <span class="literal">null</span>;</span><br><span class="line">			<span class="keyword">var</span> target = json[attr];</span><br><span class="line">			<span class="keyword">var</span> speed = <span class="literal">null</span>;</span><br><span class="line">			<span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;</span><br><span class="line">				curr = getStyle(obj,attr)*<span class="number">100</span>;</span><br><span class="line">				speed = (target*<span class="number">100</span>-curr)*<span class="number">0.15</span>;</span><br><span class="line">			&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">				curr = <span class="built_in">parseInt</span>(getStyle(obj,attr));</span><br><span class="line">				speed = (target - curr)*<span class="number">0.15</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			speed = speed&gt;<span class="number">0</span> ? <span class="built_in">Math</span>.ceil(speed):<span class="built_in">Math</span>.floor(speed);</span><br><span class="line">			<span class="keyword">if</span>(curr != target)&#123;</span><br><span class="line">				<span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;</span><br><span class="line">					obj.style[attr] = (curr+speed)/<span class="number">100</span>;</span><br><span class="line">				&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">					obj.style[attr] = curr+speed+<span class="string">"px"</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				</span><br><span class="line">				mark = <span class="literal">false</span>;</span><br><span class="line">		  &#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(mark)&#123;</span><br><span class="line">			clearInterval(obj.timer);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;,<span class="number">1000</span>/<span class="number">30</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getStyle</span>(<span class="params">obj,attr</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a href="http://codepen.io/poetries/pen/PWNKmj" target="_blank" rel="noopener">在线演示</a></p>
<ul>
<li>需要注意的地方<ul>
<li>当需要两个动画的时候，会执行后面一个，解决办法如下，回调函数</li>
<li>当需要两个以上的时候，需要考虑是否可写一行代码变换多个属性</li>
<li>变换不一致的时候，定时器被提前清除</li>
<li>速度<code>speed</code>不要写死</li>
</ul>
</li>
</ul>
<h4 id="1-3-运动框架之应用"><a href="#1-3-运动框架之应用" class="headerlink" title="1.3 运动框架之应用"></a>1.3 运动框架之应用</h4><h5 id="1-3-1-分享按钮"><a href="#1-3-1-分享按钮" class="headerlink" title="1.3.1 分享按钮"></a>1.3.1 分享按钮</h5><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"ball"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box1"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box2"</span>&gt;</span>分享到<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> box1 = <span class="built_in">document</span>.getElementById(<span class="string">"box1"</span>);</span><br><span class="line"><span class="keyword">var</span> ball = <span class="built_in">document</span>.getElementById(<span class="string">"ball"</span>);</span><br><span class="line"></span><br><span class="line">box1.onmouseover = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  move(<span class="keyword">this</span>,<span class="string">"left"</span>,<span class="number">0</span>,<span class="number">10</span>);</span><br><span class="line">&#125;</span><br><span class="line">box1.onmouseout = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  move(<span class="keyword">this</span>,<span class="string">"left"</span>,<span class="number">-100</span>,<span class="number">-10</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//问题一：当需要两个动画的时候，会执行后面一个，解决办法如下，回调函数</span></span><br><span class="line">ball.onmouseover = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="comment">//同时变换 用的最多</span></span><br><span class="line">  <span class="comment">//move(this,"width",500,10);</span></span><br><span class="line">  <span class="comment">//move(this,"height",500,10);</span></span><br><span class="line">  <span class="comment">//列队在执行</span></span><br><span class="line">  move(ball,<span class="string">"width"</span>,<span class="number">500</span>,<span class="number">10</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    move(ball,<span class="string">"height"</span>,<span class="number">500</span>,<span class="number">10</span>);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line">ball.onmouseout = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="comment">//move(this,"width",100,-10);</span></span><br><span class="line">  <span class="comment">//move(this,"height",100,-10);</span></span><br><span class="line">  move(ball,<span class="string">"width"</span>,<span class="number">100</span>,<span class="number">-10</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    move(ball,<span class="string">"height"</span>,<span class="number">100</span>,<span class="number">-10</span>);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> timer = <span class="literal">null</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params">obj,attr,target,speed,callback</span>)</span>&#123;</span><br><span class="line">  clearInterval(timer); <span class="comment">//obj.timer缓存到各自的obj下</span></span><br><span class="line">  timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> curr = <span class="built_in">parseInt</span>(getStyle(obj,attr));</span><br><span class="line">    <span class="keyword">if</span>(curr == target)&#123;</span><br><span class="line">      clearInterval(timer);</span><br><span class="line">      callback &amp;&amp; callback();</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">      obj.style[attr] = curr+speed+<span class="string">"px"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,<span class="number">1000</span>/<span class="number">30</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getStyle</span>(<span class="params">obj,attr</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a href="http://codepen.io/poetries/pen/LxNjzp" target="_blank" rel="noopener">在线演示</a></p>
<h5 id="1-3-2运动框架之轮播图应用"><a href="#1-3-2运动框架之轮播图应用" class="headerlink" title="1.3.2运动框架之轮播图应用"></a>1.3.2运动框架之轮播图应用</h5><ul>
<li><a href="http://codepen.io/poetries/pen/JEXyaG" target="_blank" rel="noopener">焦点轮播–基本版本–在线演示</a></li>
<li><a href="http://codepen.io/poetries/pen/apNyPv" target="_blank" rel="noopener">焦点轮播–淡入淡出–在线演示</a></li>
<li><a href="http://codepen.io/poetries/pen/ggrxqo" target="_blank" rel="noopener">焦点轮播–左右–在线演示</a></li>
<li><a href="http://codepen.io/poetries/pen/mRPMoq" target="_blank" rel="noopener"> 焦点轮播–上下–在线演示</a></li>
</ul>
<h6 id="1-3-2-1-焦点轮播–左右-无缝-速度版实现"><a href="#1-3-2-1-焦点轮播–左右-无缝-速度版实现" class="headerlink" title="1.3.2.1 焦点轮播–左右-无缝-速度版实现"></a>1.3.2.1 焦点轮播–左右-无缝-速度版实现</h6><hr>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"imgBox"</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">li</span>&gt;</span>![](http://upload-images.jianshu.io/upload_images/1480597-c72819402fb928e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">li</span>&gt;</span>![](http://upload-images.jianshu.io/upload_images/1480597-6830ca74fe1e6fcd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">li</span>&gt;</span>![](http://upload-images.jianshu.io/upload_images/1480597-5d38376e63ffd0b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">li</span>&gt;</span>![](http://upload-images.jianshu.io/upload_images/1480597-2aa932ffbba4091e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">li</span>&gt;</span>![](http://upload-images.jianshu.io/upload_images/1480597-c72819402fb928e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">ol</span> <span class="attr">id</span>=<span class="string">"btn"</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">"active"</span>&gt;</span>1<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">li</span>&gt;</span>2<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">li</span>&gt;</span>3<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">li</span>&gt;</span>4<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">ol</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"http://7xq6al.com1.z0.glb.clouddn.com/Animate.min.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> box = <span class="built_in">document</span>.querySelector(<span class="string">"#box"</span>);</span></span><br><span class="line"><span class="javascript">	<span class="keyword">var</span> imgUl = <span class="built_in">document</span>.querySelector(<span class="string">"#imgBox"</span>);</span></span><br><span class="line"><span class="javascript">	<span class="keyword">var</span> btns = <span class="built_in">document</span>.querySelector(<span class="string">"#btn"</span>).querySelectorAll(<span class="string">"li"</span>);</span></span><br><span class="line"><span class="javascript">	<span class="keyword">var</span> len = btns.length;</span></span><br><span class="line"><span class="javascript">	<span class="keyword">var</span> lenImg = imgUl.querySelectorAll(<span class="string">"li"</span>).length;</span></span><br><span class="line"><span class="javascript">	<span class="keyword">var</span> index = <span class="number">0</span>; <span class="comment">//控制img的索引</span></span></span><br><span class="line"><span class="javascript">	<span class="keyword">var</span> cindex = <span class="number">0</span>;<span class="comment">//控制按钮的索引</span></span></span><br><span class="line"><span class="javascript">	<span class="keyword">var</span> timer = <span class="literal">null</span>;</span></span><br><span class="line">	</span><br><span class="line"><span class="javascript">	<span class="keyword">for</span> (<span class="keyword">var</span> i=<span class="number">0</span>;i&lt;len;i++) &#123;</span></span><br><span class="line"><span class="javascript">			(<span class="function"><span class="keyword">function</span>(<span class="params">index</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">				btns[index].onmouseover = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">					<span class="keyword">for</span> (<span class="keyword">var</span> j=<span class="number">0</span>;j&lt;len;j++)&#123;</span></span><br><span class="line"><span class="javascript">						btns[j].className = <span class="string">""</span>;</span></span><br><span class="line">				&#125;</span><br><span class="line"><span class="javascript">					cindex = index;<span class="comment">//保持索引同步</span></span></span><br><span class="line"><span class="javascript">					animateSpeed(imgUl,&#123;<span class="string">"left"</span>:<span class="number">-970</span>*index&#125;);</span></span><br><span class="line"><span class="javascript">					<span class="keyword">this</span>.className = <span class="string">"active"</span>;</span></span><br><span class="line">				&#125;</span><br><span class="line">			&#125;)(i);</span><br><span class="line">	&#125;</span><br><span class="line"><span class="javascript">	<span class="function"><span class="keyword">function</span> <span class="title">autoPlay</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line">		index++;</span><br><span class="line">		cindex++;</span><br><span class="line"><span class="javascript">		cindex %=len;<span class="comment">//限制长度</span></span></span><br><span class="line"><span class="javascript">		<span class="keyword">for</span> (<span class="keyword">var</span> j=<span class="number">0</span>;j&lt;len;j++)&#123;</span></span><br><span class="line"><span class="javascript">			btns[j].className = <span class="string">""</span>;</span></span><br><span class="line">		&#125;</span><br><span class="line"><span class="javascript">		animateSpeed(imgUl,&#123;<span class="string">"left"</span>:<span class="number">-970</span>*index&#125;,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line">			</span><br><span class="line">			if(index == lenImg-1)&#123;</span><br><span class="line"><span class="javascript">				<span class="keyword">this</span>.style.left = <span class="number">0</span>;</span></span><br><span class="line">				index = 0;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;);</span><br><span class="line"><span class="javascript">		btns[cindex].className = <span class="string">"active"</span>;</span></span><br><span class="line">	&#125;</span><br><span class="line">	timer = setInterval(autoPlay,2000);</span><br><span class="line"><span class="javascript">	box.onmouseover = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line">		clearInterval(timer);</span><br><span class="line">	&#125;</span><br><span class="line"><span class="javascript">	box.onmouseout = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line">		timer = setInterval(autoPlay,2000);</span><br><span class="line">	&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line">*&#123;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">margin</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-tag">body</span>&#123;</span><br><span class="line">  <span class="attribute">font-size</span>: <span class="number">14px</span>;</span><br><span class="line">  <span class="attribute">font-family</span>: <span class="string">"微软雅黑"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-tag">ul</span>,<span class="selector-tag">li</span>&#123;</span><br><span class="line">  <span class="attribute">list-style</span>: none;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-id">#box</span> &#123;</span><br><span class="line">  <span class="attribute">position</span>: relative;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">970px</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">350px</span>;</span><br><span class="line">  <span class="attribute">margin</span>: <span class="number">30px</span> auto;</span><br><span class="line">  <span class="attribute">overflow</span>: hidden;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-id">#imgBox</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>:<span class="number">1000%</span>;<span class="comment">/*自动计算百分比*/</span></span><br><span class="line">  <span class="attribute">position</span>: absolute;</span><br><span class="line">  <span class="attribute">left</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-id">#imgBox</span> <span class="selector-tag">li</span>&#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">970px</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">350px</span>;</span><br><span class="line">  <span class="attribute">float</span>: left;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-id">#imgBox</span> <span class="selector-tag">li</span> <span class="selector-tag">img</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">970px</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">350px</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-id">#btn</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">120px</span>;</span><br><span class="line">  <span class="attribute">position</span>: absolute;</span><br><span class="line">  <span class="attribute">right</span>: <span class="number">10px</span>;</span><br><span class="line">  <span class="attribute">bottom</span>: <span class="number">10px</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-id">#btn</span> <span class="selector-tag">li</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">20px</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">20px</span>;</span><br><span class="line">  <span class="attribute">line-height</span>: <span class="number">20px</span>;</span><br><span class="line">  <span class="attribute">border-radius</span>: <span class="number">50%</span>;</span><br><span class="line">  <span class="attribute">text-align</span>: center;</span><br><span class="line">  <span class="attribute">cursor</span>: pointer;</span><br><span class="line">  <span class="attribute">background</span>: <span class="number">#fff</span>;</span><br><span class="line">  <span class="attribute">margin</span>: <span class="number">0</span> <span class="number">2px</span>;</span><br><span class="line">  <span class="attribute">float</span>: left;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-id">#btn</span> <span class="selector-tag">li</span><span class="selector-class">.active</span> &#123;</span><br><span class="line">  <span class="attribute">background</span>: <span class="number">#F17A5C</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#fff</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">//速度版本</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">win</span>)</span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params">obj,json,callback</span>)</span>&#123;</span><br><span class="line">        clearInterval(obj.timer);</span><br><span class="line">        obj.timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">var</span> mark = <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">var</span> attr <span class="keyword">in</span> json)&#123;</span><br><span class="line">                <span class="keyword">var</span> cur = <span class="literal">null</span>;</span><br><span class="line">                <span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;</span><br><span class="line">                    cur = getStyle(obj,attr)*<span class="number">100</span>;</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    <span class="comment">//如果没写 默认填充成0</span></span><br><span class="line">                    cur = <span class="built_in">parseInt</span>(getStyle(obj,attr))||<span class="number">0</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">var</span> target = json[attr];</span><br><span class="line">                <span class="keyword">var</span> speed = (target - cur)*<span class="number">0.2</span>;</span><br><span class="line">                speed = speed&gt;<span class="number">0</span>?<span class="built_in">Math</span>.ceil(speed):<span class="built_in">Math</span>.floor(speed);</span><br><span class="line">                <span class="keyword">if</span>(cur != target)&#123;</span><br><span class="line">                    <span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;</span><br><span class="line">                        <span class="comment">//IE opacity兼容问题</span></span><br><span class="line">                        obj.style.filter = <span class="string">"alpha(opacity="</span>+(cur+speed)+<span class="string">")"</span>;</span><br><span class="line">                        obj.style[attr] = (cur + speed)/<span class="number">100</span>;</span><br><span class="line">                    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                        obj.style[attr] = cur + speed + <span class="string">"px"</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    mark = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">                &#125;;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(mark)&#123;</span><br><span class="line">                clearInterval(obj.timer);</span><br><span class="line">                callback &amp;&amp; callback.call(obj);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,<span class="number">1000</span>/<span class="number">30</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    win.animateSpeed = move;</span><br><span class="line">&#125;)(<span class="built_in">window</span>);</span><br><span class="line"></span><br><span class="line"> 	</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getStyle</span>(<span class="params">obj,attr</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getId</span>(<span class="params">id</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">document</span>.getElementById(id);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="二、JavaScript运动框架之时间版"><a href="#二、JavaScript运动框架之时间版" class="headerlink" title="二、JavaScript运动框架之时间版"></a>二、JavaScript运动框架之时间版</h3><hr>
<h4 id="2-1-关于运动"><a href="#2-1-关于运动" class="headerlink" title="2.1 关于运动"></a>2.1 关于运动</h4><hr>
<ul>
<li>速度的运动 通过速度来控制元素的 位移 / 折叠 / 淡入淡出</li>
<li>时间的运动 通过时间来控制元素的 位移 / 折叠 / 淡入淡出(jQuery)</li>
<li>时间的运动 基于一些数学公式 匀速运动 在路程的每一个点 速度都一样</li>
</ul>
<h4 id="2-2-一些案例演示"><a href="#2-2-一些案例演示" class="headerlink" title="2.2 一些案例演示"></a>2.2 一些案例演示</h4><hr>
<ul>
<li><a href="http://codepen.io/poetries/pen/jyqGPR" target="_blank" rel="noopener">运动框架之时间版本-1</a></li>
<li><a href="http://codepen.io/poetries/pen/apNLZB" target="_blank" rel="noopener">运动框架之时间版本-2</a></li>
<li><a href="http://codepen.io/poetries/pen/zNqEKB" target="_blank" rel="noopener">运动框架之时间版本-3</a></li>
<li><a href="http://codepen.io/poetries/pen/MJyEbY" target="_blank" rel="noopener">运动框架之时间版本-4</a></li>
</ul>
<h4 id="2-3-运动框架之时间版本-借助animate一些函数实现–综合完整版"><a href="#2-3-运动框架之时间版本-借助animate一些函数实现–综合完整版" class="headerlink" title="2.3 运动框架之时间版本-借助animate一些函数实现–综合完整版"></a>2.3 运动框架之时间版本-借助animate一些函数实现–综合完整版</h4><hr>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box2"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box3"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box4"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box5"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="javascript"> <span class="comment">//时间版本</span></span></span><br><span class="line"><span class="javascript"> getId(<span class="string">"box2"</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript"> 	animateTime(getId(<span class="string">"box2"</span>),&#123;</span></span><br><span class="line"><span class="javascript"> 		<span class="string">"left"</span>:<span class="number">500</span>,</span></span><br><span class="line"><span class="javascript"> 		<span class="string">"opacity"</span>:<span class="number">100</span></span></span><br><span class="line"><span class="javascript"> 	&#125;,<span class="number">1000</span>,<span class="string">"elasticOut"</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript"> 		<span class="keyword">this</span>.innerHTML = <span class="string">"我是时间版本"</span>;</span></span><br><span class="line"> 	&#125;);</span><br><span class="line"> &#125;</span><br><span class="line"><span class="javascript">getId(<span class="string">"box3"</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript"> 	animateTime(getId(<span class="string">"box3"</span>),&#123;</span></span><br><span class="line"><span class="javascript"> 		<span class="string">"left"</span>:<span class="number">500</span>,</span></span><br><span class="line"><span class="javascript"> 		<span class="string">"opacity"</span>:<span class="number">100</span></span></span><br><span class="line"><span class="javascript"> 	&#125;,<span class="number">1000</span>,<span class="string">"backIn"</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript"> 		<span class="keyword">this</span>.innerHTML = <span class="string">"我是时间版本"</span>;</span></span><br><span class="line"> 	&#125;);</span><br><span class="line"> &#125;</span><br><span class="line"><span class="javascript">getId(<span class="string">"box4"</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript"> 	animateTime(getId(<span class="string">"box4"</span>),&#123;</span></span><br><span class="line"><span class="javascript"> 		<span class="string">"left"</span>:<span class="number">500</span>,</span></span><br><span class="line"><span class="javascript"> 		<span class="string">"opacity"</span>:<span class="number">100</span></span></span><br><span class="line"><span class="javascript"> 	&#125;,<span class="number">1000</span>,<span class="string">"bounceIn"</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript"> 		<span class="keyword">this</span>.innerHTML = <span class="string">"我是时间版本"</span>;</span></span><br><span class="line"> 	&#125;);</span><br><span class="line"> &#125;</span><br><span class="line"><span class="javascript">getId(<span class="string">"box5"</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript"> 	animateTime(getId(<span class="string">"box5"</span>),&#123;</span></span><br><span class="line"><span class="javascript"> 		<span class="string">"left"</span>:<span class="number">500</span>,</span></span><br><span class="line"><span class="javascript"> 		<span class="string">"opacity"</span>:<span class="number">100</span></span></span><br><span class="line"><span class="javascript"> 	&#125;,<span class="number">1000</span>,<span class="string">"bounceBoth"</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript"> 		<span class="keyword">this</span>.innerHTML = <span class="string">"我是时间版本"</span>;</span></span><br><span class="line"> 	&#125;);</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line"><span class="selector-id">#box1</span>,<span class="selector-id">#box2</span>,<span class="selector-id">#box3</span>,<span class="selector-id">#box4</span>,<span class="selector-id">#box5</span> &#123;</span><br><span class="line">	<span class="attribute">position</span>: relative;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">line-height</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">text-align</span>: center;</span><br><span class="line">	<span class="attribute">background</span>: red;</span><br><span class="line">	<span class="attribute">color</span>: <span class="number">#fff</span>;</span><br><span class="line">	<span class="attribute">font-size</span>: <span class="number">12px</span>;</span><br><span class="line">	<span class="attribute">opacity</span>: <span class="number">0.5</span>;</span><br><span class="line">	<span class="attribute">filter</span>:<span class="built_in">alpha</span>(opcity=<span class="number">20</span>);<span class="comment">/**兼容IE*/</span></span><br><span class="line">	<span class="attribute">margin</span>: <span class="number">10px</span>;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/*t b c d</span></span><br><span class="line"><span class="comment">t current time   :nTime-sTime</span></span><br><span class="line"><span class="comment">b begining time  :curr</span></span><br><span class="line"><span class="comment">c chang in value :变化量end-curr</span></span><br><span class="line"><span class="comment">d duration       :持续时间 time */</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* </span></span><br><span class="line"><span class="comment">* <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>obj 元素对象</span></span><br><span class="line"><span class="comment">* <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>json 多个属性</span></span><br><span class="line"><span class="comment">* <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>time 变化时间</span></span><br><span class="line"><span class="comment">* <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>prop 运动函数</span></span><br><span class="line"><span class="comment">* <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>callback 回调函数</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">//时间版本</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">win</span>)</span>&#123; </span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params">obj,json,time,prop,callback</span>)</span>&#123;</span><br><span class="line"><span class="comment">//一般定时器结束后最好清除</span></span><br><span class="line">clearInterval(obj.timer);</span><br><span class="line"><span class="keyword">var</span> curr = &#123;&#125;;</span><br><span class="line"><span class="keyword">var</span> end = &#123;&#125;;</span><br><span class="line"><span class="comment">//通过for in 在上车前把所有东西装到包里</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> attr <span class="keyword">in</span> json)&#123;</span><br><span class="line">	<span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;<span class="comment">//opacity特殊东西特殊对待</span></span><br><span class="line">		curr[attr] = getStyle(obj,attr)*<span class="number">100</span>;<span class="comment">//化为整数好计算</span></span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		curr[attr] = <span class="built_in">parseInt</span>(getStyle(obj,attr))||<span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	end[attr] = json[attr];</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果没写默认值 默认就是0 不然在IE出问题</span></span><br><span class="line"><span class="comment">//var curr = parseInt(getStyle(obj,attr))||0;</span></span><br><span class="line"><span class="comment">//var end = target;</span></span><br><span class="line"><span class="keyword">var</span> sTime = <span class="keyword">new</span> <span class="built_in">Date</span>();<span class="comment">//开始时间T0</span></span><br><span class="line"><span class="comment">//开始变换了</span></span><br><span class="line">obj.timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> nTime = <span class="keyword">new</span> <span class="built_in">Date</span>();<span class="comment">//当前时间Tt</span></span><br><span class="line">	<span class="keyword">var</span> t = nTime -sTime;</span><br><span class="line">	<span class="keyword">var</span> d = time;</span><br><span class="line">	<span class="comment">//St = (Tt-T0)/Time*(S-S0)+S0</span></span><br><span class="line">	<span class="comment">//(nTime-sTime)/time 比例最多为1</span></span><br><span class="line">	<span class="comment">/*var prop = (nTime-sTime)/time; */</span></span><br><span class="line">	<span class="keyword">if</span>(t &gt;=d)&#123;</span><br><span class="line">		t = d;</span><br><span class="line">		clearInterval(obj.timer);</span><br><span class="line">		callback &amp;&amp; callback.call(obj);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">var</span> attr <span class="keyword">in</span> json)&#123;</span><br><span class="line">		<span class="keyword">var</span> b = curr[attr];</span><br><span class="line">		<span class="keyword">var</span> c = end[attr] - b;</span><br><span class="line">		<span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;</span><br><span class="line">			<span class="comment">//var s = prop*(end[attr]-curr[attr])+curr[attr];</span></span><br><span class="line">			<span class="keyword">var</span> s = Tween[prop](t,b,c,d);</span><br><span class="line">			obj.style[attr] = s/<span class="number">100</span>;</span><br><span class="line">			obj.style.filter = <span class="string">"alpha(opacity="</span>+s+<span class="string">")"</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			<span class="comment">//var s = prop*(end[attr]-curr[attr])+curr[attr];</span></span><br><span class="line">			<span class="keyword">var</span> s = Tween[prop](t,b,c,d);</span><br><span class="line">			obj.style[attr] = s+<span class="string">"px"</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">&#125;,<span class="number">13</span>);</span><br><span class="line"><span class="keyword">var</span> Tween = &#123;</span><br><span class="line">    linear: <span class="function"><span class="keyword">function</span> (<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//匀速</span></span><br><span class="line">        <span class="keyword">return</span> c*t/d + b;   <span class="comment">//  t/d = prop;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    easeIn: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//加速曲线</span></span><br><span class="line">        <span class="keyword">return</span> c*(t/=d)*t + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    easeOut: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//减速曲线</span></span><br><span class="line">        <span class="keyword">return</span> -c *(t/=d)*(t<span class="number">-2</span>) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    easeBoth: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//加速减速曲线</span></span><br><span class="line">        <span class="keyword">if</span> ((t/=d/<span class="number">2</span>) &lt; <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> c/<span class="number">2</span>*t*t + b;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> -c/<span class="number">2</span> * ((--t)*(t<span class="number">-2</span>) - <span class="number">1</span>) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    easeInStrong: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//加加速曲线</span></span><br><span class="line">        <span class="keyword">return</span> c*(t/=d)*t*t*t + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    easeOutStrong: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//减减速曲线</span></span><br><span class="line">        <span class="keyword">return</span> -c * ((t=t/d<span class="number">-1</span>)*t*t*t - <span class="number">1</span>) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    easeBothStrong: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//加加速减减速曲线</span></span><br><span class="line">        <span class="keyword">if</span> ((t/=d/<span class="number">2</span>) &lt; <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> c/<span class="number">2</span>*t*t*t*t + b;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> -c/<span class="number">2</span> * ((t-=<span class="number">2</span>)*t*t*t - <span class="number">2</span>) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    elasticIn: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, a, p</span>)</span>&#123;  <span class="comment">//正弦衰减曲线（弹动渐入）</span></span><br><span class="line">        <span class="keyword">if</span> (t === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> b;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> ( (t /= d) == <span class="number">1</span> ) &#123;</span><br><span class="line">            <span class="keyword">return</span> b+c;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!p) &#123;</span><br><span class="line">            p=d*<span class="number">0.3</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!a || a &lt; <span class="built_in">Math</span>.abs(c)) &#123;</span><br><span class="line">            a = c;</span><br><span class="line">            <span class="keyword">var</span> s = p/<span class="number">4</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">var</span> s = p/(<span class="number">2</span>*<span class="built_in">Math</span>.PI) * <span class="built_in">Math</span>.asin (c/a);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> -(a*<span class="built_in">Math</span>.pow(<span class="number">2</span>,<span class="number">10</span>*(t-=<span class="number">1</span>)) * <span class="built_in">Math</span>.sin( (t*d-s)*(<span class="number">2</span>*<span class="built_in">Math</span>.PI)/p )) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    elasticOut: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, a, p</span>)</span>&#123;    <span class="comment">//正弦增强曲线（弹动渐出）</span></span><br><span class="line">        <span class="keyword">if</span> (t === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> b;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> ( (t /= d) == <span class="number">1</span> ) &#123;</span><br><span class="line">            <span class="keyword">return</span> b+c;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!p) &#123;</span><br><span class="line">            p=d*<span class="number">0.3</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!a || a &lt; <span class="built_in">Math</span>.abs(c)) &#123;</span><br><span class="line">            a = c;</span><br><span class="line">            <span class="keyword">var</span> s = p / <span class="number">4</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">var</span> s = p/(<span class="number">2</span>*<span class="built_in">Math</span>.PI) * <span class="built_in">Math</span>.asin (c/a);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> a*<span class="built_in">Math</span>.pow(<span class="number">2</span>,<span class="number">-10</span>*t) * <span class="built_in">Math</span>.sin( (t*d-s)*(<span class="number">2</span>*<span class="built_in">Math</span>.PI)/p ) + c + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    elasticBoth: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, a, p</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (t === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> b;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> ( (t /= d/<span class="number">2</span>) == <span class="number">2</span> ) &#123;</span><br><span class="line">            <span class="keyword">return</span> b+c;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!p) &#123;</span><br><span class="line">            p = d*(<span class="number">0.3</span>*<span class="number">1.5</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> ( !a || a &lt; <span class="built_in">Math</span>.abs(c) ) &#123;</span><br><span class="line">            a = c;</span><br><span class="line">            <span class="keyword">var</span> s = p/<span class="number">4</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">var</span> s = p/(<span class="number">2</span>*<span class="built_in">Math</span>.PI) * <span class="built_in">Math</span>.asin (c/a);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (t &lt; <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> - <span class="number">0.5</span>*(a*<span class="built_in">Math</span>.pow(<span class="number">2</span>,<span class="number">10</span>*(t-=<span class="number">1</span>)) *</span><br><span class="line">                    <span class="built_in">Math</span>.sin( (t*d-s)*(<span class="number">2</span>*<span class="built_in">Math</span>.PI)/p )) + b;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> a*<span class="built_in">Math</span>.pow(<span class="number">2</span>,<span class="number">-10</span>*(t-=<span class="number">1</span>)) *</span><br><span class="line">                <span class="built_in">Math</span>.sin( (t*d-s)*(<span class="number">2</span>*<span class="built_in">Math</span>.PI)/p )*<span class="number">0.5</span> + c + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    backIn: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, s</span>)</span>&#123;     <span class="comment">//回退加速（回退渐入）</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">typeof</span> s == <span class="string">'undefined'</span>) &#123;</span><br><span class="line">           s = <span class="number">1.70158</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> c*(t/=d)*t*((s+<span class="number">1</span>)*t - s) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    backOut: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, s</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">typeof</span> s == <span class="string">'undefined'</span>) &#123;</span><br><span class="line">            s = <span class="number">3.70158</span>;  <span class="comment">//回缩的距离</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> c*((t=t/d<span class="number">-1</span>)*t*((s+<span class="number">1</span>)*t + s) + <span class="number">1</span>) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    backBoth: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, s</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">typeof</span> s == <span class="string">'undefined'</span>) &#123;</span><br><span class="line">            s = <span class="number">1.70158</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> ((t /= d/<span class="number">2</span> ) &lt; <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> c/<span class="number">2</span>*(t*t*(((s*=(<span class="number">1.525</span>))+<span class="number">1</span>)*t - s)) + b;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> c/<span class="number">2</span>*((t-=<span class="number">2</span>)*t*(((s*=(<span class="number">1.525</span>))+<span class="number">1</span>)*t + s) + <span class="number">2</span>) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    bounceIn: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;    <span class="comment">//弹球减振（弹球渐出）</span></span><br><span class="line">        <span class="keyword">return</span> c - Tween[<span class="string">'bounceOut'</span>](d-t, <span class="number">0</span>, c, d) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    bounceOut: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ((t/=d) &lt; (<span class="number">1</span>/<span class="number">2.75</span>)) &#123;</span><br><span class="line">            <span class="keyword">return</span> c*(<span class="number">7.5625</span>*t*t) + b;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (t &lt; (<span class="number">2</span>/<span class="number">2.75</span>)) &#123;</span><br><span class="line">            <span class="keyword">return</span> c*(<span class="number">7.5625</span>*(t-=(<span class="number">1.5</span>/<span class="number">2.75</span>))*t + <span class="number">0.75</span>) + b;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (t &lt; (<span class="number">2.5</span>/<span class="number">2.75</span>)) &#123;</span><br><span class="line">            <span class="keyword">return</span> c*(<span class="number">7.5625</span>*(t-=(<span class="number">2.25</span>/<span class="number">2.75</span>))*t + <span class="number">0.9375</span>) + b;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> c*(<span class="number">7.5625</span>*(t-=(<span class="number">2.625</span>/<span class="number">2.75</span>))*t + <span class="number">0.984375</span>) + b;</span><br><span class="line">    &#125;,</span><br><span class="line">    bounceBoth: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (t &lt; d/<span class="number">2</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> Tween[<span class="string">'bounceIn'</span>](t*<span class="number">2</span>, <span class="number">0</span>, c, d) * <span class="number">0.5</span> + b;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> Tween[<span class="string">'bounceOut'</span>](t*<span class="number">2</span>-d, <span class="number">0</span>, c, d) * <span class="number">0.5</span> + c*<span class="number">0.5</span> + b;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;;</span><br><span class="line">&#125;</span><br><span class="line">win.animateTime = move;	</span><br><span class="line">&#125;)(<span class="built_in">window</span>);</span><br></pre></td></tr></table></figure>
<p><a href="http://codepen.io/poetries/pen/ggrGLj" target="_blank" rel="noopener">在线演示</a></p>
<h3 id="三、运动框架之时间速度版完整封装"><a href="#三、运动框架之时间速度版完整封装" class="headerlink" title="三、运动框架之时间速度版完整封装"></a>三、运动框架之时间速度版完整封装</h3><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">/*t b c d</span></span><br><span class="line"><span class="comment">t current time   :nTime-sTime</span></span><br><span class="line"><span class="comment">b begining time  :curr</span></span><br><span class="line"><span class="comment">c chang in value :变化量end-curr</span></span><br><span class="line"><span class="comment">d duration       :持续时间 time */</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>obj 元素对象</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>json 多个属性</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>time 变化时间</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>prop 运动函数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>callback 回调函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//时间版本</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">win</span>)</span>&#123; </span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params">obj,json,time,prop,callback</span>)</span>&#123;</span><br><span class="line">	<span class="comment">//一般定时器结束后最好清除</span></span><br><span class="line">	clearInterval(obj.timer);</span><br><span class="line">	<span class="keyword">var</span> curr = &#123;&#125;;</span><br><span class="line">	<span class="keyword">var</span> end = &#123;&#125;;</span><br><span class="line">	<span class="comment">//通过for in 在上车前把所有东西装到包里</span></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">var</span> attr <span class="keyword">in</span> json)&#123;</span><br><span class="line">		<span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;<span class="comment">//opacity特殊东西特殊对待</span></span><br><span class="line">			curr[attr] = getStyle(obj,attr)*<span class="number">100</span>;<span class="comment">//化为整数好计算</span></span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			curr[attr] = <span class="built_in">parseInt</span>(getStyle(obj,attr))||<span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		end[attr] = json[attr];</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//如果没写默认值 默认就是0 不然在IE出问题</span></span><br><span class="line">	<span class="comment">//var curr = parseInt(getStyle(obj,attr))||0;</span></span><br><span class="line">	<span class="comment">//var end = target;</span></span><br><span class="line">	<span class="keyword">var</span> sTime = <span class="keyword">new</span> <span class="built_in">Date</span>();<span class="comment">//开始时间T0</span></span><br><span class="line">	<span class="comment">//开始变换了</span></span><br><span class="line">	obj.timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">var</span> nTime = <span class="keyword">new</span> <span class="built_in">Date</span>();<span class="comment">//当前时间Tt</span></span><br><span class="line">		<span class="keyword">var</span> t = nTime -sTime;</span><br><span class="line">		<span class="keyword">var</span> d = time;</span><br><span class="line">		<span class="comment">//St = (Tt-T0)/Time*(S-S0)+S0</span></span><br><span class="line">		<span class="comment">//(nTime-sTime)/time 比例最多为1</span></span><br><span class="line">		<span class="comment">/*var prop = (nTime-sTime)/time; */</span></span><br><span class="line">		<span class="keyword">if</span>(t &gt;=d)&#123;</span><br><span class="line">			t = d;</span><br><span class="line">			clearInterval(obj.timer);</span><br><span class="line">			callback &amp;&amp; callback.call(obj);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">var</span> attr <span class="keyword">in</span> json)&#123;</span><br><span class="line">			<span class="keyword">var</span> b = curr[attr];</span><br><span class="line">			<span class="keyword">var</span> c = end[attr] - b;</span><br><span class="line">			<span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;</span><br><span class="line">				<span class="comment">//var s = prop*(end[attr]-curr[attr])+curr[attr];</span></span><br><span class="line">				<span class="keyword">var</span> s = Tween[prop](t,b,c,d);</span><br><span class="line">				obj.style[attr] = s/<span class="number">100</span>;</span><br><span class="line">				obj.style.filter = <span class="string">"alpha(opacity="</span>+s+<span class="string">")"</span>;</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				<span class="comment">//var s = prop*(end[attr]-curr[attr])+curr[attr];</span></span><br><span class="line">				<span class="keyword">var</span> s = Tween[prop](t,b,c,d);</span><br><span class="line">				obj.style[attr] = s+<span class="string">"px"</span>;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		</span><br><span class="line">	&#125;,<span class="number">13</span>);</span><br><span class="line">	<span class="keyword">var</span> Tween = &#123;</span><br><span class="line">        linear: <span class="function"><span class="keyword">function</span> (<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//匀速</span></span><br><span class="line">            <span class="keyword">return</span> c*t/d + b;   <span class="comment">//  t/d = prop;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        easeIn: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//加速曲线</span></span><br><span class="line">            <span class="keyword">return</span> c*(t/=d)*t + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        easeOut: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//减速曲线</span></span><br><span class="line">            <span class="keyword">return</span> -c *(t/=d)*(t<span class="number">-2</span>) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        easeBoth: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//加速减速曲线</span></span><br><span class="line">            <span class="keyword">if</span> ((t/=d/<span class="number">2</span>) &lt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> c/<span class="number">2</span>*t*t + b;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> -c/<span class="number">2</span> * ((--t)*(t<span class="number">-2</span>) - <span class="number">1</span>) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        easeInStrong: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//加加速曲线</span></span><br><span class="line">            <span class="keyword">return</span> c*(t/=d)*t*t*t + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        easeOutStrong: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//减减速曲线</span></span><br><span class="line">            <span class="keyword">return</span> -c * ((t=t/d<span class="number">-1</span>)*t*t*t - <span class="number">1</span>) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        easeBothStrong: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;  <span class="comment">//加加速减减速曲线</span></span><br><span class="line">            <span class="keyword">if</span> ((t/=d/<span class="number">2</span>) &lt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> c/<span class="number">2</span>*t*t*t*t + b;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> -c/<span class="number">2</span> * ((t-=<span class="number">2</span>)*t*t*t - <span class="number">2</span>) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        elasticIn: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, a, p</span>)</span>&#123;  <span class="comment">//正弦衰减曲线（弹动渐入）</span></span><br><span class="line">            <span class="keyword">if</span> (t === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> b;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ( (t /= d) == <span class="number">1</span> ) &#123;</span><br><span class="line">                <span class="keyword">return</span> b+c;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!p) &#123;</span><br><span class="line">                p=d*<span class="number">0.3</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!a || a &lt; <span class="built_in">Math</span>.abs(c)) &#123;</span><br><span class="line">                a = c;</span><br><span class="line">                <span class="keyword">var</span> s = p/<span class="number">4</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">var</span> s = p/(<span class="number">2</span>*<span class="built_in">Math</span>.PI) * <span class="built_in">Math</span>.asin (c/a);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> -(a*<span class="built_in">Math</span>.pow(<span class="number">2</span>,<span class="number">10</span>*(t-=<span class="number">1</span>)) * <span class="built_in">Math</span>.sin( (t*d-s)*(<span class="number">2</span>*<span class="built_in">Math</span>.PI)/p )) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        elasticOut: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, a, p</span>)</span>&#123;    <span class="comment">//正弦增强曲线（弹动渐出）</span></span><br><span class="line">            <span class="keyword">if</span> (t === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> b;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ( (t /= d) == <span class="number">1</span> ) &#123;</span><br><span class="line">                <span class="keyword">return</span> b+c;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!p) &#123;</span><br><span class="line">                p=d*<span class="number">0.3</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!a || a &lt; <span class="built_in">Math</span>.abs(c)) &#123;</span><br><span class="line">                a = c;</span><br><span class="line">                <span class="keyword">var</span> s = p / <span class="number">4</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">var</span> s = p/(<span class="number">2</span>*<span class="built_in">Math</span>.PI) * <span class="built_in">Math</span>.asin (c/a);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> a*<span class="built_in">Math</span>.pow(<span class="number">2</span>,<span class="number">-10</span>*t) * <span class="built_in">Math</span>.sin( (t*d-s)*(<span class="number">2</span>*<span class="built_in">Math</span>.PI)/p ) + c + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        elasticBoth: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, a, p</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (t === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> b;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ( (t /= d/<span class="number">2</span>) == <span class="number">2</span> ) &#123;</span><br><span class="line">                <span class="keyword">return</span> b+c;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!p) &#123;</span><br><span class="line">                p = d*(<span class="number">0.3</span>*<span class="number">1.5</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ( !a || a &lt; <span class="built_in">Math</span>.abs(c) ) &#123;</span><br><span class="line">                a = c;</span><br><span class="line">                <span class="keyword">var</span> s = p/<span class="number">4</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">var</span> s = p/(<span class="number">2</span>*<span class="built_in">Math</span>.PI) * <span class="built_in">Math</span>.asin (c/a);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (t &lt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> - <span class="number">0.5</span>*(a*<span class="built_in">Math</span>.pow(<span class="number">2</span>,<span class="number">10</span>*(t-=<span class="number">1</span>)) *</span><br><span class="line">                        <span class="built_in">Math</span>.sin( (t*d-s)*(<span class="number">2</span>*<span class="built_in">Math</span>.PI)/p )) + b;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> a*<span class="built_in">Math</span>.pow(<span class="number">2</span>,<span class="number">-10</span>*(t-=<span class="number">1</span>)) *</span><br><span class="line">                    <span class="built_in">Math</span>.sin( (t*d-s)*(<span class="number">2</span>*<span class="built_in">Math</span>.PI)/p )*<span class="number">0.5</span> + c + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        backIn: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, s</span>)</span>&#123;     <span class="comment">//回退加速（回退渐入）</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">typeof</span> s == <span class="string">'undefined'</span>) &#123;</span><br><span class="line">               s = <span class="number">1.70158</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> c*(t/=d)*t*((s+<span class="number">1</span>)*t - s) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        backOut: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, s</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">typeof</span> s == <span class="string">'undefined'</span>) &#123;</span><br><span class="line">                s = <span class="number">3.70158</span>;  <span class="comment">//回缩的距离</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> c*((t=t/d<span class="number">-1</span>)*t*((s+<span class="number">1</span>)*t + s) + <span class="number">1</span>) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        backBoth: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d, s</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">typeof</span> s == <span class="string">'undefined'</span>) &#123;</span><br><span class="line">                s = <span class="number">1.70158</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ((t /= d/<span class="number">2</span> ) &lt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> c/<span class="number">2</span>*(t*t*(((s*=(<span class="number">1.525</span>))+<span class="number">1</span>)*t - s)) + b;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> c/<span class="number">2</span>*((t-=<span class="number">2</span>)*t*(((s*=(<span class="number">1.525</span>))+<span class="number">1</span>)*t + s) + <span class="number">2</span>) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        bounceIn: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;    <span class="comment">//弹球减振（弹球渐出）</span></span><br><span class="line">            <span class="keyword">return</span> c - Tween[<span class="string">'bounceOut'</span>](d-t, <span class="number">0</span>, c, d) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        bounceOut: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">if</span> ((t/=d) &lt; (<span class="number">1</span>/<span class="number">2.75</span>)) &#123;</span><br><span class="line">                <span class="keyword">return</span> c*(<span class="number">7.5625</span>*t*t) + b;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (t &lt; (<span class="number">2</span>/<span class="number">2.75</span>)) &#123;</span><br><span class="line">                <span class="keyword">return</span> c*(<span class="number">7.5625</span>*(t-=(<span class="number">1.5</span>/<span class="number">2.75</span>))*t + <span class="number">0.75</span>) + b;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (t &lt; (<span class="number">2.5</span>/<span class="number">2.75</span>)) &#123;</span><br><span class="line">                <span class="keyword">return</span> c*(<span class="number">7.5625</span>*(t-=(<span class="number">2.25</span>/<span class="number">2.75</span>))*t + <span class="number">0.9375</span>) + b;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> c*(<span class="number">7.5625</span>*(t-=(<span class="number">2.625</span>/<span class="number">2.75</span>))*t + <span class="number">0.984375</span>) + b;</span><br><span class="line">        &#125;,</span><br><span class="line">        bounceBoth: <span class="function"><span class="keyword">function</span>(<span class="params">t, b, c, d</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (t &lt; d/<span class="number">2</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> Tween[<span class="string">'bounceIn'</span>](t*<span class="number">2</span>, <span class="number">0</span>, c, d) * <span class="number">0.5</span> + b;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> Tween[<span class="string">'bounceOut'</span>](t*<span class="number">2</span>-d, <span class="number">0</span>, c, d) * <span class="number">0.5</span> + c*<span class="number">0.5</span> + b;</span><br><span class="line">            &#125;</span><br><span class="line">    &#125;;</span><br><span class="line"> &#125;</span><br><span class="line">	win.animateTime = move;	</span><br><span class="line"> &#125;)(<span class="built_in">window</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//速度版本</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">win</span>)</span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params">obj,json,callback</span>)</span>&#123;</span><br><span class="line">        clearInterval(obj.timer);</span><br><span class="line">        obj.timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">var</span> mark = <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">var</span> attr <span class="keyword">in</span> json)&#123;</span><br><span class="line">                <span class="keyword">var</span> cur = <span class="literal">null</span>;</span><br><span class="line">                <span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;</span><br><span class="line">                    cur = getStyle(obj,attr)*<span class="number">100</span>;</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    <span class="comment">//如果没写 默认填充成0</span></span><br><span class="line">                    cur = <span class="built_in">parseInt</span>(getStyle(obj,attr))||<span class="number">0</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">var</span> target = json[attr];</span><br><span class="line">                <span class="keyword">var</span> speed = (target - cur)*<span class="number">0.2</span>;</span><br><span class="line">                speed = speed&gt;<span class="number">0</span>?<span class="built_in">Math</span>.ceil(speed):<span class="built_in">Math</span>.floor(speed);</span><br><span class="line">                <span class="keyword">if</span>(cur != target)&#123;</span><br><span class="line">                    <span class="keyword">if</span>(attr == <span class="string">"opacity"</span>)&#123;</span><br><span class="line">                        <span class="comment">//IE opacity兼容问题</span></span><br><span class="line">                        obj.style.filter = <span class="string">"alpha(opacity="</span>+(cur+speed)+<span class="string">")"</span>;</span><br><span class="line">                        obj.style[attr] = (cur + speed)/<span class="number">100</span>;</span><br><span class="line">                    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                        obj.style[attr] = cur + speed + <span class="string">"px"</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    mark = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">                &#125;;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(mark)&#123;</span><br><span class="line">                clearInterval(obj.timer);</span><br><span class="line">                callback &amp;&amp; callback.call(obj);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,<span class="number">1000</span>/<span class="number">30</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    win.animateSpeed = move;</span><br><span class="line">&#125;)(<span class="built_in">window</span>);</span><br><span class="line"></span><br><span class="line"> 	</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getStyle</span>(<span class="params">obj,attr</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getId</span>(<span class="params">id</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">document</span>.getElementById(id);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>更多使用记录，详情 <a href="https://github.com/poetries/Animate" target="_blank" rel="noopener">https://github.com/poetries/Animate</a></li>
</ul>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2017/01/10/js-scope/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2017/01/13/gulp/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
